package tf.mcs.imp.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Service;
import tf.mcs.imp.entity.CnIljsChnl;
import tf.mcs.imp.entity.CnIljsParserdef;
import tf.mcs.imp.entity.CnIljsSvrcatalog;
import tf.mcs.imp.entity.CnIljsSvrdef;
import tf.mcs.imp.service.ImpService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Zhiwei Wang
 * @version ：$
 * @ClassName ：ImpServiceImpl
 * @description ：
 * @date ：2022/11/14 18:16
 * @history ：
 */
@Service
public class ImpServiceImpl implements ImpService {

    @Override
    public List<CnIljsSvrdef> getCnIljsSvrdefs(Map<String, List<LinkedHashMap>> sheetInfos, List<CnIljsSvrcatalog> svrcatalogs) {
        List<CnIljsSvrdef> svrdefs = new ArrayList<>();
        for (CnIljsSvrcatalog svrcatalog : svrcatalogs) {
            String provcsmflg = svrcatalog.getProvcsmflg();
            String svrcode = svrcatalog.getSvrcode();
            String svrname = svrcatalog.getSvrname();
            String ispub = svrcatalog.getIspub();
            String sndflg = svrcatalog.getSndflg();

            String svrcode1 = svrcode.replace("_send", "")
                    .replace("_rec", "");
            String svrname1 = svrname.replace("-请求", "")
                    .replace("-响应", "");

            List<LinkedHashMap> linkedHashMaps = sheetInfos.get(svrcode1 + "-" + svrname1);

            String chnlcode = svrcatalog.getChnlcode();
//处理标志 0-不处理;1-请求报文;2-响应报文
            int flag = 0;

            for (LinkedHashMap row : linkedHashMaps) {
                String flagStr = (String) row.get("0");
                if (row.size() > 0) {
                    if ("报文体-输入".equals(flagStr)) {
                        flag = 1;
                        continue;
                    } else if ("报文体-输出".equals(flagStr)) {
                        flag = 2;
                        continue;
                    }

                    String itemind = (String) row.get("6");
                    String moflg = ("Y".equals(row.get("4"))) ? "M" : "O";
                    if (flag == 1 && ("0".equals(sndflg)||"4".equals(sndflg))) {
                        CnIljsSvrdef cnIljs = new CnIljsSvrdef();
                        cnIljs.setChnlcode(chnlcode);
                        cnIljs.setSvrcode(svrcode);
                        cnIljs.setItemcode((String) row.get("0"));
                        cnIljs.setItemname((String) row.get("1"));

                        if ("0-N".equals(itemind)) {
                            cnIljs.setItemind("2");
                        } else if ("0-1".equals(itemind)) {
                            cnIljs.setItemind("3");
                        } else {
                            cnIljs.setItemind("1");
                        }
                        cnIljs.setMoflg(moflg);
                        cnIljs.setItemlen((String) row.get("3"));
                        if ("1".equals(provcsmflg)) {
                            cnIljs.setSndflg("4");
                        } else {
                            cnIljs.setSndflg("0");
                        }
                        cnIljs.setExtCode((String) row.get("5"));
                        cnIljs.setPrantItemcode((String) row.get("7"));
                        svrdefs.add(cnIljs);
                    } else if (flag == 2 && ("1".equals(sndflg)||"3".equals(sndflg))) {
                        CnIljsSvrdef cnIljs = new CnIljsSvrdef();
                        cnIljs.setChnlcode(chnlcode);
                        cnIljs.setSvrcode(svrcode);
                        cnIljs.setItemcode((String) row.get("0"));
                        cnIljs.setItemname((String) row.get("1"));

                        if ("0-N".equals(itemind)) {
                            cnIljs.setItemind("2");
                        } else if ("0-1".equals(itemind)) {
                            cnIljs.setItemind("3");
                        } else {
                            cnIljs.setItemind("1");
                        }
                        cnIljs.setMoflg(moflg);
                        cnIljs.setItemlen((String) row.get("3"));
                        if ("1".equals(provcsmflg)) {
                            cnIljs.setSndflg("3");
                        } else {
                            cnIljs.setSndflg("1");
                        }
                        cnIljs.setExtCode((String) row.get("5"));
                        cnIljs.setPrantItemcode((String) row.get("7"));
                        svrdefs.add(cnIljs);
                    }
                }
            }
        }
        return svrdefs;
    }

    @Override
    public List<CnIljsParserdef> getParserdefs(List<LinkedHashMap> sheets) {
        List<CnIljsParserdef> cnIljsParserdefs = new ArrayList<>();
        for (LinkedHashMap sheet : sheets) {
            String bankId = (String) sheet.get("0");
            String svrcode = (String) sheet.get("2");
            String csmOrProv = (String) sheet.get("4");
            String isPub = (String) sheet.get("5");
            if ("否".equals(isPub)) {
                if ("消费".equals(csmOrProv)) {
                    CnIljsParserdef cnIljsParserdef = new CnIljsParserdef();
                    cnIljsParserdef.setBankid(new BigDecimal(bankId));
                    cnIljsParserdef.setSvrcode(svrcode);
                    cnIljsParserdef.setProvcsmflg("2");
                    cnIljsParserdef.setSndflg("0");
                    cnIljsParserdef.setSort(1);
                    cnIljsParserdef.setQuosc("csm_pub_send");
                    cnIljsParserdefs.add(cnIljsParserdef);
                    cnIljsParserdef = new CnIljsParserdef();
                    cnIljsParserdef.setBankid(new BigDecimal(bankId));
                    cnIljsParserdef.setSvrcode(svrcode);
                    cnIljsParserdef.setProvcsmflg("2");
                    cnIljsParserdef.setSndflg("0");
                    cnIljsParserdef.setSort(2);
                    cnIljsParserdef.setQuosc(svrcode);
                    cnIljsParserdefs.add(cnIljsParserdef);

                    cnIljsParserdef = new CnIljsParserdef();
                    cnIljsParserdef.setBankid(new BigDecimal(bankId));
                    cnIljsParserdef.setSvrcode(svrcode);
                    cnIljsParserdef.setProvcsmflg("2");
                    cnIljsParserdef.setSndflg("1");
                    cnIljsParserdef.setSort(3);
                    cnIljsParserdef.setQuosc("csm_pub_rec");
                    cnIljsParserdefs.add(cnIljsParserdef);
                    cnIljsParserdef = new CnIljsParserdef();
                    cnIljsParserdef.setBankid(new BigDecimal(bankId));
                    cnIljsParserdef.setSvrcode(svrcode);
                    cnIljsParserdef.setProvcsmflg("2");
                    cnIljsParserdef.setSndflg("1");
                    cnIljsParserdef.setSort(4);
                    cnIljsParserdef.setQuosc(svrcode);
                    cnIljsParserdefs.add(cnIljsParserdef);
                } else {
                    CnIljsParserdef cnIljsParserdef = new CnIljsParserdef();
                    cnIljsParserdef.setBankid(new BigDecimal(bankId));
                    cnIljsParserdef.setSvrcode(svrcode);
                    cnIljsParserdef.setProvcsmflg("1");
                    cnIljsParserdef.setSndflg("3");
                    cnIljsParserdef.setSort(1);
                    cnIljsParserdef.setQuosc("prov_pub_send");
                    cnIljsParserdefs.add(cnIljsParserdef);
                    cnIljsParserdef = new CnIljsParserdef();
                    cnIljsParserdef.setBankid(new BigDecimal(bankId));
                    cnIljsParserdef.setSvrcode(svrcode);
                    cnIljsParserdef.setProvcsmflg("1");
                    cnIljsParserdef.setSndflg("3");
                    cnIljsParserdef.setSort(2);
                    cnIljsParserdef.setQuosc(svrcode);
                    cnIljsParserdefs.add(cnIljsParserdef);

                    cnIljsParserdef = new CnIljsParserdef();
                    cnIljsParserdef.setBankid(new BigDecimal(bankId));
                    cnIljsParserdef.setSvrcode(svrcode);
                    cnIljsParserdef.setProvcsmflg("1");
                    cnIljsParserdef.setSndflg("4");
                    cnIljsParserdef.setSort(3);
                    cnIljsParserdef.setQuosc("prov_pub_rec");
                    cnIljsParserdefs.add(cnIljsParserdef);
                    cnIljsParserdef = new CnIljsParserdef();
                    cnIljsParserdef.setBankid(new BigDecimal(bankId));
                    cnIljsParserdef.setSvrcode(svrcode);
                    cnIljsParserdef.setProvcsmflg("1");
                    cnIljsParserdef.setSndflg("4");
                    cnIljsParserdef.setSort(4);
                    cnIljsParserdef.setQuosc(svrcode);
                    cnIljsParserdefs.add(cnIljsParserdef);
                }
            }
        }
        return cnIljsParserdefs;
    }

    @Override
    public List<CnIljsChnl> getCnIljsChnl(List<LinkedHashMap> sheets) {
        List<CnIljsChnl> cnIljsChnls = new ArrayList<>();
        for (LinkedHashMap sheet : sheets) {
            CnIljsChnl cnIljsChnl = new CnIljsChnl();
            cnIljsChnl.setChnlcode((String) sheet.get("0"));
            cnIljsChnl.setChnlname((String) sheet.get("1"));
            cnIljsChnl.setTargetaddr((String) sheet.get("2"));
            cnIljsChnl.setTimeout(Integer.parseInt((String) sheet.get("3")));
            cnIljsChnl.setCurver((String) sheet.get("4"));
            cnIljsChnl.setChnlenc((String) sheet.get("5"));
            cnIljsChnl.setHttpprtc((String) sheet.get("6"));
            cnIljsChnls.add(cnIljsChnl);
        }
        return cnIljsChnls;
    }

    @Override
    public List<CnIljsSvrcatalog> getCnIljsCatalogs(List<LinkedHashMap> sheets) {
        List<CnIljsSvrcatalog> svrcatalogs = new ArrayList<>();
        for (LinkedHashMap sheet : sheets) {
            String bankId = (String) sheet.get("0");
            String chnlcode = (String) sheet.get("1");
            String svrcode = (String) sheet.get("2");
            String svrName = (String) sheet.get("3");
            String csmOrProv = (String) sheet.get("4");
            String isPub = (String) sheet.get("5");
            String baffleFlag = "";

            if ("是".equals(sheet.get("6"))) {
                baffleFlag = "1";
            } else {
                baffleFlag = "0";
            }

            if ("是".equals(isPub)) {
                if ("消费".equals(csmOrProv)) {
                    CnIljsSvrcatalog svrcatalog = new CnIljsSvrcatalog();
                    svrcatalog.setBankid(new BigDecimal(bankId));
                    svrcatalog.setChnlcode(chnlcode);
                    svrcatalog.setSvrcode(svrcode + "_send");
                    svrcatalog.setSvrname(svrName + "-请求");
                    svrcatalog.setProvcsmflg("2");
                    svrcatalog.setIspub("1");
                    svrcatalog.setIsact("1");
                    svrcatalog.setBdate("20200101");
                    svrcatalog.setEdate("99999999");
                    svrcatalog.setSndflg("0");
                    svrcatalog.setBaffleFlag(baffleFlag);
                    svrcatalogs.add(svrcatalog);
                    svrcatalog = new CnIljsSvrcatalog();
                    svrcatalog.setBankid(new BigDecimal(bankId));
                    svrcatalog.setChnlcode(chnlcode);
                    svrcatalog.setSvrcode(svrcode + "_rec");
                    svrcatalog.setSvrname(svrName + "-响应");
                    svrcatalog.setProvcsmflg("2");
                    svrcatalog.setIspub("1");
                    svrcatalog.setIsact("1");
                    svrcatalog.setBdate("20200101");
                    svrcatalog.setEdate("99999999");
                    svrcatalog.setSndflg("1");
                    svrcatalog.setBaffleFlag(baffleFlag);
                    svrcatalogs.add(svrcatalog);
                } else {
                    CnIljsSvrcatalog svrcatalog = new CnIljsSvrcatalog();
                    svrcatalog.setBankid(new BigDecimal(bankId));
                    svrcatalog.setChnlcode(chnlcode);
                    svrcatalog.setSvrcode(svrcode + "_send");
                    svrcatalog.setSvrname(svrName + "-响应");
                    svrcatalog.setProvcsmflg("1");
                    svrcatalog.setIspub("1");
                    svrcatalog.setIsact("1");
                    svrcatalog.setBdate("20200101");
                    svrcatalog.setEdate("99999999");
                    svrcatalog.setSndflg("3");
                    svrcatalog.setBaffleFlag(baffleFlag);
                    svrcatalogs.add(svrcatalog);
                    svrcatalog = new CnIljsSvrcatalog();
                    svrcatalog.setBankid(new BigDecimal(bankId));
                    svrcatalog.setChnlcode(chnlcode);
                    svrcatalog.setSvrcode(svrcode + "_rec");
                    svrcatalog.setSvrname(svrName + "-请求");
                    svrcatalog.setProvcsmflg("1");
                    svrcatalog.setIspub("1");
                    svrcatalog.setIsact("1");
                    svrcatalog.setBdate("20200101");
                    svrcatalog.setEdate("99999999");
                    svrcatalog.setSndflg("4");
                    svrcatalog.setBaffleFlag(baffleFlag);
                    svrcatalogs.add(svrcatalog);
                }
            } else {
                if ("消费".equals(csmOrProv)) {
                    CnIljsSvrcatalog svrcatalog = new CnIljsSvrcatalog();
                    svrcatalog.setBankid(new BigDecimal(bankId));
                    svrcatalog.setChnlcode(chnlcode);
                    svrcatalog.setSvrcode(svrcode);
                    svrcatalog.setSvrname(svrName + "-请求");
                    svrcatalog.setProvcsmflg("2");
                    svrcatalog.setIspub("0");
                    svrcatalog.setIsact("1");
                    svrcatalog.setBdate("20200101");
                    svrcatalog.setEdate("99999999");
                    svrcatalog.setSndflg("0");
                    svrcatalog.setBaffleFlag(baffleFlag);
                    svrcatalogs.add(svrcatalog);
                    svrcatalog = new CnIljsSvrcatalog();
                    svrcatalog.setBankid(new BigDecimal(bankId));
                    svrcatalog.setChnlcode(chnlcode);
                    svrcatalog.setSvrcode(svrcode);
                    svrcatalog.setSvrname(svrName + "-响应");
                    svrcatalog.setProvcsmflg("2");
                    svrcatalog.setIspub("0");
                    svrcatalog.setIsact("1");
                    svrcatalog.setBdate("20200101");
                    svrcatalog.setEdate("99999999");
                    svrcatalog.setSndflg("1");
                    svrcatalog.setBaffleFlag(baffleFlag);
                    svrcatalogs.add(svrcatalog);
                } else {
                    CnIljsSvrcatalog svrcatalog = new CnIljsSvrcatalog();
                    svrcatalog.setBankid(new BigDecimal(bankId));
                    svrcatalog.setChnlcode(chnlcode);
                    svrcatalog.setSvrcode(svrcode);
                    svrcatalog.setSvrname(svrName + "-请求");
                    svrcatalog.setProvcsmflg("1");
                    svrcatalog.setIspub("0");
                    svrcatalog.setIsact("1");
                    svrcatalog.setBdate("20200101");
                    svrcatalog.setEdate("99999999");
                    svrcatalog.setSndflg("4");
                    svrcatalog.setBaffleFlag(baffleFlag);
                    svrcatalogs.add(svrcatalog);
                    svrcatalog = new CnIljsSvrcatalog();
                    svrcatalog.setBankid(new BigDecimal(bankId));
                    svrcatalog.setChnlcode(chnlcode);
                    svrcatalog.setSvrcode(svrcode);
                    svrcatalog.setSvrname(svrName + "-响应");
                    svrcatalog.setProvcsmflg("1");
                    svrcatalog.setIspub("0");
                    svrcatalog.setIsact("1");
                    svrcatalog.setBdate("20200101");
                    svrcatalog.setEdate("99999999");
                    svrcatalog.setSndflg("3");
                    svrcatalog.setBaffleFlag(baffleFlag);
                    svrcatalogs.add(svrcatalog);
                }
            }
        }
        return svrcatalogs;
    }

    @Override
    public List<CnIljsSvrdef> getDataFormat(List<CnIljsSvrdef> svrdefs) {
//获取父节点
        for (CnIljsSvrdef svrdef : svrdefs) {
            if ("root".equals(svrdef.getPrantItemcode())) {
                if ("0".equals(svrdef.getSndflg()) || "3".equals(svrdef.getSndflg())) {
                    getValidChildrens(svrdef, svrdefs, svrdef.getItemcode());
                    svrdef.setDataFormat(svrdef.getItemcode());
                } else {
                    svrdef.setDataFormat(svrdef.getExtCode());
                    getValidChildrens(svrdef, svrdefs, svrdef.getExtCode());
                }

            }
        }
        return svrdefs;
    }

    /**
     * 递归查询子节点
     *
     * @param root 根节点
     * @param all  所有节点
     * @return 根节点信息
     */
    private void getValidChildrens(CnIljsSvrdef root, List<CnIljsSvrdef> all, String dataFormat) {
        for (CnIljsSvrdef child : all) {
            if (child.getPrantItemcode().equals(root.getItemcode()) && child.getSvrcode().equals(root.getSvrcode())&& child.getSndflg().equals(root.getSndflg())) {
                if ("0".equals(child.getSndflg()) || "3".equals(child.getSndflg())) {
                    child.setDataFormat(dataFormat + "/" + child.getItemcode());
                    getValidChildrens(child, all, dataFormat + "/" + child.getItemcode());
                } else {
                    child.setDataFormat(dataFormat + "/" + child.getExtCode());
                    getValidChildrens(child, all, dataFormat + "/" + child.getExtCode());
                }

            }
        }
    }
}



